package com.mj.activity.publish;

import java.io.File;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.Window;
import android.view.WindowManager;

import com.mj.R;
import com.mj.base.BaseActivity;
import com.mj.util.DensityUtil;
import com.mj.util.FileUtil;

//选择上传图片来源
public class PicActivity extends BaseActivity {
	private final int CHOICEPHOTO = 1, TAKEPHOTO = 2, PHOTORESOULT = 3;
	private final String IMAGE_UNSPECIFIED = "image/*";

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_pic);
		init();
	}

	private void init() {
		// 将页面宽度设置为全屏
		WindowManager.LayoutParams p = getWindow().getAttributes();
		p.width = (int) (DensityUtil.getDisplay(this).widthPixels * 0.9);
		p.height = (int) (DensityUtil.getDisplay(this).heightPixels * 0.9);
		// 设置生效
		getWindow().setAttributes(p);
		// 去除背景框
		getWindow().setBackgroundDrawableResource(android.R.color.transparent);
		// 图片选择方式
		aq.id(R.id.imageView1).clicked(this, "choicephoto");
		aq.id(R.id.imageView2).clicked(this, "takephoto");
	}

	/***
	 * 图库选择
	 */
	public void choicephoto() {
		Intent intent = new Intent();
		intent.setType("image/*");
		intent.setAction(Intent.ACTION_GET_CONTENT);
		startActivityForResult(intent, CHOICEPHOTO);
	}

	/***
	 * 拍照
	 */
	public void takephoto() {
		Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
		startActivityForResult(intent, TAKEPHOTO);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode != RESULT_OK || data == null) {
			return;
		}
		Bitmap photo;
		switch (requestCode) {
		case CHOICEPHOTO:
			Uri uri = data.getData();
			startPhotoZoom(uri);
			break;
		case TAKEPHOTO:
			photo = data.getExtras().getParcelable("data");
			FileUtil.saveBitmapToFile(photo, "/temp.jpg");
			File tempFile = new File(Environment.getExternalStorageDirectory() + "/temp.jpg");
			startPhotoZoom(Uri.fromFile(tempFile));
			break;
		case PHOTORESOULT:
			photo = data.getExtras().getParcelable("data");
			FileUtil.saveBitmapToFile(photo, "/temp_update.jpg");
			setResult(RESULT_OK, data);
			finish();
			break;
		}

	}

	/***
	 * 裁剪图片页面
	 * 
	 * @param uri
	 */
	private void startPhotoZoom(Uri uri) {
		Intent intent = new Intent("com.android.camera.action.CROP");
		intent.setDataAndType(uri, IMAGE_UNSPECIFIED);
		intent.putExtra("crop", "true");
		// aspectX aspectY 是宽高的比例
		intent.putExtra("aspectX", 1);
		intent.putExtra("aspectY", 1);
		// outputX outputY 是裁剪图片宽高
		intent.putExtra("outputX", 96);
		intent.putExtra("outputY", 96);
		intent.putExtra("return-data", true);
		startActivityForResult(intent, PHOTORESOULT);
	}
}
